package com.appkqxs.paser;

import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.appkqxs.enity.YoutubeEntity;

public class YoutubeLoad {

	public static ArrayList<YoutubeEntity> listYoutube = new ArrayList<YoutubeEntity>();
	private static ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
	private static ArrayList<String> listUser = new ArrayList<String>();
	public void getMoreVideo(String url)  {
		try {
			if(!url.equals("http://www.youtube.com")) {
				URL oracle = new URL(url);
				URLConnection connection = oracle.openConnection();
				connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31");
			    Document doc = Jsoup.parse(connection.getInputStream(),"UTF-8","application/json; charset=UTF-8");
			    Elements els = doc.select("body");
			    JSONObject jsonObject = JSONObject.fromObject(els.text());
			  
			    Document docParse = Jsoup.parse(jsonObject.get("content_html").toString());
			    Elements elsParse = docParse.select("a[href*=/watch?v]:has(img)");
			    
			    Document docLinkMore = Jsoup.parse(jsonObject.get("load_more_widget_html").toString());
			    Elements elsButton = docLinkMore.select("button[data-uix-load-more-href]");
			    String linkMore = "http://www.youtube.com"+elsButton.attr("data-uix-load-more-href").trim();
			    System.out.println("Link more: "+linkMore);
			    
			    getListChilrent(elsParse);
			    
			    getMoreVideo(linkMore);
			    
			}
		} catch (Exception e) {
			System.out.println("Error Get More: "+e.getMessage());
		}
		
	   
	    
	    
	}
	private String[] getTitleAndDescription(String url) {
		
		String[] str = new String[2];
		try {
			Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31").get();
			Elements els = doc.select("meta[name=description]");
			str[0] = new String(doc.title().trim());
			str[1] = new String(els.attr("content").trim());
		} catch (Exception e) {
			System.out.println("Error - Link: "+ url + " - " +e.getMessage());
			str[0] = new String();
			str[1] = new String();
		}
		
		return str;
	}
	
	public void getListChilrent(Elements els) throws Exception {
		for (Element element : els) {
			//Thread.sleep(300);
			if(element.attr("href").contains("list=")) {
				continue;
			}
			String link = "http://www.youtube.com"+element.attr("href");
			String img = "";
			String title = "";
			String[] titleDesctiption = getTitleAndDescription(link);
			String description = titleDesctiption[1];
			if(element.select("img").attr("data-thumb").trim().equals("")) {
				img = element.select("img").attr("src");
			} else {
				img = element.select("img").attr("data-thumb");
			}
			if(element.select("span.title").attr("title").trim().equals("")) {
				title = titleDesctiption[0].replaceAll("- YouTube", "");
			} else {
				title = element.select("span.title").attr("title").trim();
			}
			
			YoutubeEntity  youtubeEntity = new YoutubeEntity();
			youtubeEntity.setLink(link);
			youtubeEntity.setImage(img);
			youtubeEntity.setTitle(title);
			youtubeEntity.setDescription(description);
			if(!listYoutube.contains(youtubeEntity)) {
				System.out.println("Link:" + link);
				System.out.println("Image:" + img);
				System.out.println("Title:" + title);
				System.out.println("Description:" + description);
				System.out.println("----------------------");
				
				listYoutube.add(youtubeEntity);
				concurrentLinkedQueue.add(link);
			}
			

		}
		
	}
	public void getListYou(String url) throws Exception {

		Document doc = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31").get();
		Elements els = doc.select("a[href*=/watch?v]:has(img)");
		
		Elements elsButton = doc.select("button[data-uix-load-more-href]");
		String linkMore = "http://www.youtube.com"+elsButton.attr("data-uix-load-more-href").trim();
		System.out.println("Link more: "+linkMore);
		Elements elsUser = doc.select("a[href~=/(user|channel)/\\w+(/videos)?$]");
		for (Element element : elsUser) {
			String linkUser;
			if(element.attr("href").trim().matches("/(user|channel)/\\w+/videos$")) {
				linkUser = "http://www.youtube.com"+element.attr("href").trim();
			} else {
				linkUser = "http://www.youtube.com"+element.attr("href").trim()+"/videos";
			}
			//System.out.println(linkUser);
			if(!listUser.contains(linkUser)) {
				listUser.add(linkUser);
				concurrentLinkedQueue.add(linkUser);
			}
		}
		for (Element element : els) {
			//Thread.sleep(300);
			if(element.attr("href").contains("list=")) {
				continue;
			}
			String link = "http://www.youtube.com"+element.attr("href");
			String img = "";
			String title = "";
			String[] titleDesctiption = getTitleAndDescription(link);
			String description = titleDesctiption[1];
			if(element.select("img").attr("data-thumb").trim().equals("")) {
				img = element.select("img").attr("src");
			} else {
				img = element.select("img").attr("data-thumb");
			}
			if(element.select("span.title").attr("title").trim().equals("")) {
				title = titleDesctiption[0].replaceAll("- YouTube", "");
			} else {
				title = element.select("span.title").attr("title").trim();
			}
			
			YoutubeEntity  youtubeEntity = new YoutubeEntity();
			youtubeEntity.setLink(link);
			youtubeEntity.setImage(img);
			youtubeEntity.setTitle(title);
			youtubeEntity.setDescription(description);
			if(!listYoutube.contains(youtubeEntity)) {
				System.out.println("Link:" + link);
				System.out.println("Image:" + img);
				System.out.println("Title:" + title);
				System.out.println("Description:" + description);
				System.out.println("----------------------");
				listYoutube.add(youtubeEntity);
				concurrentLinkedQueue.add(link);
				
			}
			
		}
		getMoreVideo(linkMore);
	}
	public void getQueue() {
		
		concurrentLinkedQueue.add("http://www.youtube.com/user/TaylorSwiftVEVO/videos");
		concurrentLinkedQueue.add("http://www.youtube.com/user/ShayneWardVEVO/videos");
		concurrentLinkedQueue.add("http://www.youtube.com/user/BridgitMendlerVEVO/videos");
		concurrentLinkedQueue.add("http://www.youtube.com/user/SelenaGomezVEVO/videos");
		concurrentLinkedQueue.add("http://www.youtube.com/user/TRESEMME/videos");
		//System.out.println(concurrentLinkedQueue.poll());
		//System.out.println(concurrentLinkedQueue.size());
//		for (String string : concurrentLinkedQueue) {
//			System.out.println(string);
//		}
		
	}
	class ThreadGetLink implements Runnable {
		private String link;
		public ThreadGetLink(String link) {
			this.link = link;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			System.out.println(link);
			try {
				getListYou(link);
			} catch (Exception e) {
				System.out.println("Error: "+e.getMessage());
			}
			
		}
		
	}
	public void threadQueue() {
		getQueue();
		
		int threadSize = 5;
		while(true) {
			try {
				if(concurrentLinkedQueue.size() == 0) {
					System.out.println("Not found Queue");
				} else {
					
					for (int i = 0; i < threadSize; i++) {
						String link = concurrentLinkedQueue.poll();
						if(Thread.activeCount() <= threadSize) {
							Thread thread = new Thread(new ThreadGetLink(link));
							thread.start();
						}
					}
				}
				System.out.println("Thread : "+Thread.activeCount());
				System.out.println("ConcurrentLinkedQueue: "+ concurrentLinkedQueue.size());
				System.out.println("ListYoutube: "+listYoutube.size());
				System.out.println("ListUser: "+listUser.size());
			} catch (Exception e) {
				System.out.println("Error: "+e.getMessage());
			}
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				System.out.println("Error Thread Queue: "+e.getMessage());
			}
		}
	}
	public static void main(String[] args) throws Exception {

		new YoutubeLoad().threadQueue();
		//new YoutubeLoad().getListYou("http://www.youtube.com/watch?v=n-D1EB74Ckg");
		

	}

}
